package com.yanqu.road.server.cmd.unioncampwar;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;


@Cmd(code = GameProtocol.S_UNION_CAMP_WAR_OPEN_BOX, desc = "商帮宝库抽奖")
public class UnionCampWarOpenBoxCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (null == player) {
            return;
        }
        UnionCampWar.UnionCampWarOpenBoxReqMsg reqMsg = UnionCampWar.UnionCampWarOpenBoxReqMsg.parseFrom(packet.getBytes());
        Property cost = ConfigMgr.getUnionCampWarConfig().getLotteryCost();
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        player.getModule(PreReduceModule.class).preReduce(cost);

        player.sendPacket(CrossProtocol.C3_UNION_CAMP_WAR_OPEN_BOX, reqMsg.toBuilder());

    }

    @Override
    public void sendData(GamePlayer player) {
        if (null == player) {
            return;
        }
        if (0 != ret) {
            player.sendPacket(ClientProtocol.U_UNION_CAMP_WAR_OPEN_BOX, UnionCampWar.UnionCampWarAttackRespMsg.newBuilder().setRet(ret));
        }

    }
}
